package cn.cuiliangpeng.org.service.impl;

import cn.cuiliangpeng.basic.consts.RedisConstants;
import cn.cuiliangpeng.basic.service.impl.BaseServiceImpl;
import cn.cuiliangpeng.org.domain.Department;
import cn.cuiliangpeng.org.mapper.DepartmentMapper;
import cn.cuiliangpeng.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门业务处理
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)  //开启事务
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 添加部门
     * @param department
     */
    @Override
    @Transactional//事务
    @CacheEvict(cacheNames = RedisConstants.DEPT_TREE_KEY,key = "'tree'")
    public void save(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    /**
     * 修改部门
     * @param department
     */
    @Override
    @Transactional//事务
    @CacheEvict(cacheNames = RedisConstants.DEPT_TREE_KEY,key = "'tree'")
    public void update(Department department) {
        String dirPath="/";
        if (department.getParent() ==null){
            dirPath=dirPath+department.getId();//新增的是的顶级部门
        }else {
            //新增的不是顶级  需要查询出上级部门的dirPath + 自己的就可以了 根据父亲ID 查询出父亲的dirPath
            Department parent = departmentMapper.findById(department.getParent().getId());
            dirPath=parent.getDirPath() + "/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
     * 删除部门
     * @param id
     */
    @Override
    @Transactional//事务
    @CacheEvict(cacheNames = RedisConstants.DEPT_TREE_KEY,key = "'tree'")
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @CacheEvict(cacheNames = RedisConstants.DEPT_TREE_KEY,key = "'tree'")
    public void patch(Long[] ids) {
        departmentMapper.patch(ids);
    }

    /**
     * 部门树
     * @return
     */
    @Override
    @Cacheable(cacheNames = RedisConstants.DEPT_TREE_KEY,key = "'tree'")
    public List<Department> departmentTree() {
        /*//从redis中获取数据
        List<Department> result= (List<Department>)redisTemplate.opsForValue().get(RedisConstants.DEPT_TREE_KEY);
        //如果没有获取到数据
        if (result==null){
            //从数据库获取数据，
            result=getDepartmentTree();
            //将数据保存一份到redis
            redisTemplate.opsForValue().set(RedisConstants.DEPT_TREE_KEY,result);
        }
        return result;*/
        return getDepartmentTree();
    }

    private List<Department> getDepartmentTree() {
        List<Department> departments = departmentMapper.findAll();//查询所有的部门

        List<Department> list=new ArrayList<>();

        for (Department department : departments) {
            if (department.getParent_id()==null){//顶级部门
                list.add(department);//添加到部门树
            }else {//有父级部门
                for (Department panents : departments) {
                    if (panents.getId().equals(department.getParent_id())){//子部门的id等于父部门的id
                        panents.getChildren().add(department);//将自己状态父部门的children集合中
                    }
                }
            }
        }
        return list;
    }
}
